package com.hzit.order.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.order.dto.ItemDTO;
import com.hzit.order.dto.OrderDTO;
import com.hzit.order.entity.Address;
import com.hzit.order.entity.Order;
import com.hzit.order.entity.PayLog;
import com.hzit.order.service.*;
import com.hzit.order.vo.OrderVo;
import com.ruoyi.system.api.domain.PayAsyncVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wf
 * @since 2024-02-27
 */
@Controller
@RequestMapping
public class OrderController {

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private ICartService cartService;
	@Autowired
	private IPayLogService payLogService;
	@Autowired
	private IPayService payService;
	//1. 欢迎页面
	@GetMapping({"/","/getOrderInfo.html"})
	public String getOrderInfo(Model model, HttpSession session){
		//1.1 得到登录login值
		Map map  = (Map) session.getAttribute("userInfo");
		String login = map.get("login").toString();
		//1.2 根据上面的login值查询出用户地址列表
		List<Address> addresses = addressService.findAddressByUserId(login);
		//1.3 将地址列表放到model中
		model.addAttribute("addressList",addresses);
		//1.4 取出token值
		String token = session.getAttribute("token").toString();
		model.addAttribute("token",token);
		//1.5 查询出购物车信息并放到model中
		List<ItemDTO> dtos = cartService.getCartListFromRedis();
		//1.6 将上面的商品列表放到model中
		model.addAttribute("cartList",dtos);
		return "getOrderInfo";
	}

	//2. 保存订单
	@RequestMapping("/order/add")
	public String saveOrder(OrderVo vo) throws ExecutionException, InterruptedException {
		//2.1 保存订单
		orderService.saveOrder(vo);
		return "redirect:http://order.hzit.com/paypage";
	}
	//3. 到达支付页面
	@GetMapping("/paypage")
	public String payPage(Model model,HttpSession session){
		//3.1 取出支付日志中的数据并放到model中
		String login = getLoginInfo(session);
		PayLog payLog = payLogService.getPayLogFromRedis(login);
		model.addAttribute("paylog",payLog);
		return "pay";
	}

	// 获取登录用户中的login属性
	@NotNull
	private String getLoginInfo(HttpSession session) {
		return ((Map) session.getAttribute("userInfo")).get("login") + "";
	}

	//4. 异步通知
	@PostMapping("/order/notifyUrl")
	@ResponseBody
	public String notifyOrder(PayAsyncVo vo, HttpServletRequest request) throws AlipayApiException {
		//4.1  进行验签处理
		String result = payService.rsaCheck(request.getParameterMap(),vo);

		return result;
	}

	//5. 同步通知
	//功能：完成当前登录用户的订单及订单项列表展示
	@GetMapping("/order/orderlist")
	public String orderList(Model model,HttpSession session){
		//5.1  得到当前登录用户
		String login = getLoginInfo(session);
		//5.2 查询订单及订单项列表
		List<OrderDTO> dtos = orderService.findOrderListByLogin(login);
		//5.3 查询出来后放到model中
		model.addAttribute("orderList",dtos);
		//5.4  跳转到指定视图
		return "orderlist";
	}

}
